﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Interfaces;
using Helpers;
using System.ComponentModel.Composition;
using LabelPrinter.Views;
using System.Windows.Controls;
using System.Windows.Input;
using PrintManager;
using System.Windows.Documents;

namespace LabelPrinter.ViewModels
{
    [Export(typeof(IModule))]
    [ModuleMetadata(Name = "Book numbers", Short = "Numbers", Group = "Labels", IconFilename = "BookLabels.png")]
    public class LabelPrinterViewModel : NotifyPropertyChangedBase, IModule
    {
        //[Import(typeof(Action<IDocumentPaginatorSource>))]
        //public Action<IDocumentPaginatorSource> ShowPreview { get; set; }

        PrintEngine pe;
        public LayoutManager LayoutManager { get; set; }

        int _startNumber;
        public int StartNumber
        {
            get { return _startNumber; }
            set
            {
                _startNumber = value;
                NotifyPropertyChanged("StartNumber");
            }
        }

        int _labelCount;
        public int LabelCount
        {
            get { return _labelCount; }
            set
            {
                _labelCount = value;
                NotifyPropertyChanged("LabelCount");
            }
        }

        RelayCommand _printCommand;
        public ICommand PrintCommand
        {
            get
            {
                if (_printCommand == null)
                {
                    _printCommand = new RelayCommand(param => this.Print(),
                        param => this.CanPrint);
                }
                return _printCommand;
            }
        }

        //public int id { get { return 123456789; } }
        //public string bar { get { return "1234567890"; } }

        public LabelPrinterViewModel()
        {
            var data = new
            {
                id = 123456789,
                bar = "1234567890"
            };
            pe = new PrintEngine();
            LayoutManager = new LayoutManager(pe, "booknumbers", data);
        }

        private void Print()
        {
            var q = Enumerable.Range(_startNumber, _labelCount).Select(x => new { id = x, bar = string.Format("{0:00000000}", x) });
            List<object> data = new List<object>(q);
            FixedDocument doc = pe.CreateDocument(data, LayoutManager.SelectedLayout, LayoutManager.SelectedPaper);
            PrintDialog pd = new PrintDialog();
            if (pd.ShowDialog() != true)
                return;
            pd.PrintDocument(doc.DocumentPaginator, "");
            //ShowPreview(doc.DocumentPaginator.Source);
        }

        public UserControl View()
        {
            var t = new LabelPrinterView();
            t.DataContext = this;
            return t;
        }

        public bool CanPrint
        {
            get
            {
                return _labelCount <= 1000 && _labelCount > 0 && LayoutManager.SelectedLayout != null && LayoutManager.SelectedPaper != null;
            }
        }
    }
}
